@extends('layouts.header')
@section('content')
    <a class="btn btn-primary radius r" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a> <br>
    <article class="pd-10">
        <form class="form form-horizontal" id="form" autocomplete="off" >
            <div class="row cl">
                <label class="form-label col-sm-3">样品类型：</label>
                <label class="form-label col-sm-9" style="text-align: left;"> {{ $data_tool->name }}</label>
            </div>
            <div class="row cl">
                <label class="form-label col-sm-3">检验标准：</label>
                @if ($data_std->isEmpty())
                    <div class="formControls col-sm-9">
                        <span class="select-box">
                            <select class="select" name="std_id" size="1">
                                <option value="" selected>请先关联标准</option>
                            </select>
                        </span>
                    </div>
                @elseif ($data_std->count()==1)
                    <div class="formControls col-sm-9">
                        <span class="select-box">
                            <select class="select" name="std_id" size="1">
                                {{-- <option value="">--请选择--</option> --}}
                                @foreach($data_std as $val)
                                    <option value="{{$val -> id}}" selected>{{$val -> num}}</option>
                                @endforeach
                            </select>
                        </span>
                    </div>
                @else
                    <div class="formControls col-sm-9">
                        <span class="select-box">
                            <select class="select" name="std_id" size="1">
                                <option value="">--请选择--</option>
                                @foreach($data_std as $val)
                                    <option value="{{$val -> id}}">{{$val -> num}}</option>
                                @endforeach
                            </select>
                        </span>
                    </div>
                @endif
            </div>
            <div class="row cl">
                <label class="form-label col-sm-3">检验项目：</label>
                <div class="formControls col-sm-9">
                    {{-- <span class="select-box" style="width:200px;"> --}}
                        <select class="select2" name="testitem_id" style="width: 100%">
                            <option value="">--请选择--</option>
                            @foreach($data_item as $val)
                                <option value="{{$val -> id}}">{{$val -> name}}</option>
                            @endforeach
                        </select>
                    {{-- </span> --}}
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-sm-3">技术要求：</label>
                <div class="formControls col-sm-9">
                    <input type="text" class="input-text" value="" placeholder="" name="jishu_yaoqiu">
                </div>
            </div>

            @csrf

            <div class="row cl">
                <div class="col-sm-9 col-sm-offset-3">
                    <input class="btn btn-primary radius @if ($data_std->isEmpty()) disabled @endif" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
                </div>
            </div>
        </form>
        <form class="form form-horizontal mt-10" id="form-table" autocomplete="off">
            <div class="row cl">
                <label class="form-label col-sm-2 ">
                    <span class="c-blue" r>选择要修改的模板：</span>
                </label>
                <div class="formControls col-sm-2">
                    <select class="select2 input-text" id="std_num">
                        <option value="">--选择标准--</option>
                        @foreach($data_excel_pos as $std_num => $excel_pos_item )
                        <option value="{{$std_num}}">{{$std_num}}</option>
                        @endforeach
                    </select>
                </div>
                <div class="formControls col-sm-2">
                    <select class="select2 input-text" id="excel_poses" name="excel_pos_id">
                        <option value="">--选择模板--</option>
                    </select>
                </div>
                <div class="formControls r">
                    <span class="btn btn-primary radius f-r mr-15" id="save_tb2">保存排序</span>
                    <span class="btn btn-primary radius f-r mr-15" id="save_tb">保存位置</span>
                </div>
            </div>
            <table class="table table-border table-bordered table-hover table-bg table-responsive table-condensed mt-10">
                <thead>
                    <tr class="text-c">
                        <th>ID</th>
                        <th>标准代号</th>
                        <th>检验项目</th>
                        <th width="20%">技术要求</th>
                        <th width="60px">检测结果<br>开始列</th>
                        <th width="60px">检测结果<br>结束列</th>
                        <th width="60px">检测结果<br> <b class="c-red">初始行号 <br> 单行不设置</b></th>
                        <th width="60px">排序</th>
                        <th>绑定主检人</th>
                        <th>当前绑定设备</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($data_rel as $val)
                        <tr class="text-c" id="tr_{{$val->id}}">
                            <td>{{ $val->id }}</td>
                            <td class="std_num">{{ $val->num }}</td>
                            <td>{{ $val->name }}</td>
                            <td class="text-l">{{ $val->jishu_yaoqiu }}</td>
                            <td class="pos_start"><input type="text" name="data[{{ $val->std_id }}][{{ $val->id }}][pos_start]" class="input-text" style="text-align:center" value="{{ $val->pos_start }}"></td>
                            <td class="pos_end">
                                <input type="text" name="data[{{ $val->std_id }}][{{ $val->id }}][pos_end]" class="input-text" style="text-align:center" value="{{ $val->pos_end }}">
                            </td>
                            <td class="line_num">
                                <input type="text" name="data[{{ $val->std_id }}][{{ $val->id }}][line_num]" class="input-text" style="text-align:center" value="{{ $val->line_num }}">
                            </td>
                            <td>
                                <input type="text" name="data2[{{ $val->id }}][sort]" class="input-text" style="text-align:center" value="{{ $val->sort }}">
                            </td>
                            <td>{{ $val->realname }}</td>
                            <td>
                                @php
                                    $station2 = [];
                                    $station_ids = explode(',',$val->station_ids);
                                    foreach($station_ids as $station_id){
                                        $station2[] = data_get($stations,$station_id,'');   
                                    }
                                    $station2 = implode('，',$station2);
                                    echo $station2;
                                @endphp
                            </td>
                            <td>
                                @if ($val->ishide === 0)
                                    <span class="label label-success radius" title="隐藏后业务受理时不会显示">显示</span>
                                @else
                                    <span class="label label-default radius" title="隐藏后业务受理时不会显示">隐藏</span>
                                @endif
                            </td>
                            <td style="width: 200px;">
                                <a class="btn btn-primary-outline radius size-MINI" onclick="copy({{ $val->id }})">一键复制</a>
                                <a class="btn btn-primary-outline radius size-MINI" onclick="rel_xxx('关联试验条件标题','/{{config('constant.name_admin')}}/conditionKey/conditionTitle',{{ $val->id }},$(window).width() - 50,'')">试验条件</a>
                                <a class="ml-5 btn btn-primary-outline radius size-MINI" onclick="rel_xxx('关联数据标题','/{{config('constant.name_admin')}}/testdataKey/dataTitle',{{ $val->id }},'','')">试验数据</a>
                                <a class="ml-5 btn btn-primary-outline radius size-MINI" onclick="rel_xxx('对应标准项目数据','/{{config('constant.name_admin')}}/testdataKey/relation',{{ $val->id }},'','')">映射关系</a>
                                <a href="javascript:;" onclick="edit({{ $val->id }})" class="ml-5 btn btn-success-outline radius size-MINI mt-5" >修改</a>
                                <a href="javascript:;" onclick="del(this,{{ $val->id }})" class="ml-5 btn btn-danger-outline radius size-MINI mt-5" >删除</a>
                            </td>
                        </tr>
                    @endforeach
                    
                </tbody>
                @csrf
                
            </table>
        </form>
    </article>

    <script type="text/javascript">
        var dataExcelPos = @json($data_excel_pos);
        $(function(){
            
            var index2;

            $('.select2').select2();

            $("#form").validate({
                rules:{
                    std_id:{
                        required:true,
                    },
                    testitem_id:{
                        required:true,
                    },
                    jishu_yaoqiu:{
                        // required:true,
                        // notSpace:true,
                    },
                },
                // onkeyup:false,
                // focusCleanup:false,
                success:"valid",
                submitHandler:function(form){
                   $(form).ajaxSubmit({ 
                        type: 'post', 
                        url: "",//自己提交给自己可以不写url   
                        //beforeSend  beforeSubmit
                        
                        beforeSend : function () { 
                            //0.4透明度 白色遮罩
                           index2 = layer.load(2,{ shade: [0.3,'#fff'] }); 
                        },
                        complete : function () { 
                            layer.close(index2); 
                        },
                        success: function(data) {
                            //判断添加结果 
                            if(data === '1'){ 
                                window.location.reload();//刷新
                                parent.layer.msg('添加成功!',{ icon: 1, time:2000 });
                            }else if(data === '2'){ 
                                layer.msg('已存在相同的记录!',{ icon: 2, time:5000 });
                            }
                            else{
                                layer.msg('操作失败!', { icon: 2, time: 2000 }); 
                            } 
                        }, 
                        error: function(data) {
                            console.log(data);
                            ajax_error(data);
                        }///error---end
                    }); 
                }
            });
        });

        function copy(id){
            let data_rel=@json($data_rel),
                data_rel2 = {};
            $(data_rel).each(function (i,item) {
                data_rel2[item.id] = item.num+'==>'+item.name;
            })
            let content = '<div class="pd-10" style="width:500px;height:200px;">请选择要复制试验条件和数据的项目：<br>'+
                    // '<label><input type="radio" id="copy_info">覆盖项目位置和要求</label><br>'+
                    '<select id="copy_id" class="select2 input-text" style="width:300px;">';
            for(let id in data_rel2){
                content += '<option value="'+id+'">'+data_rel2[id]+'</option>';
            }
            content += '</select></div>';
            layer.open({
                type:1,
                title:'操作：',
                content:content,
                btn:['确定','取消'],
                btn1:function () {
                    ajax('post','?=copy',{action:'copy',id:id,id_map:$('#copy_id').val()});
                }
            })
        }

        /*删除*/
        function del(obj,id){
            var index2;

            layer.confirm('确认要删除吗？',function(index){
                $.ajax({
                    type: 'GET',
                    url: '/{{config('constant.name_admin')}}/stdToolItemRel/del' + '?id=' + id,
                    // dataType: 'json',
                    success: function(data){
                        // console.log(data);
                        if(data === '1'){
                            $(obj).parents("tr").remove();
                            layer.msg('删除成功!',{icon:1,time:2000});
                        }else{
                            layer.msg('删除失败!',{icon:2,time:2000});
                        }
                    },
                    error:function(data) {
                        ajax_error(data);
                    },
                    beforeSend : function () { 
                        //0.4透明度 白色遮罩
                        index2 = layer.load(2,{ shade: [0.05,'#000'] }); 
                    },
                    complete : function () { 
                        layer.close(index2); 
                    },
                });		
            });
        }

        /*关联xxx*/
        function rel_xxx(title,url,id,w,h){
            layer_show(title,url + '?id=' + id,w,h);
        }

        /* 修改 */
        function edit(id){
            var w = '';
            var h = '';
            layer_show('修改','/{{config('constant.name_admin')}}/stdToolItemRel/edit?id=' + id,w,h);
        }

        $('#std_num').on('change',function () {
            let poses = dataExcelPos[$(this).val()],
                html = '<option value="">--选择模板--</option>';
            for(let i in poses){
                html += '<option value="'+poses[i].excel_pos_id+'" '+poses[i]+'>'+poses[i].pos_name+'</option>';
            }
            $('#excel_poses').html(html);
            let std_num = $(this).find('option:selected').text().trim();
            $('.std_num','table').each(function () {
                if (std_num == $(this).text().trim()) {
                    $(this).closest('tr').show();
                }else{
                    $(this).closest('tr').hide();
                }
            })
        })
        $('#excel_poses').on('change',function() {
            let excel_pos_id = $(this).val();
            console.log(excel_pos_id);
            if(excel_pos_id == ""){
                layer.msg('请先选择要修改的模板！');
            }
            // ajax('get','excel_pos_std_tool_item',{'excel_pos_id':excel_pos_id},excel_pos_std_tool_item_bak,0);
            ajax('get','/{{config('constant.name_admin')}}/excelPos/getTemplPos',{'excel_pos_id':excel_pos_id,'type':'item'},getTemplPosBak,0);
            function getTemplPosBak(res) {
                console.log(res);
                if (res.length == 0) {
                    layer.msg('该模板暂无数据！以下为默认数据！');
                    return false;
                }
                for (var i = 0; i < res.length; i++) {
                    let item = res[i],
                        rel_id = item.std_tool_item_rel_id;
                    $('#tr_'+rel_id).find('td.pos_start').find('input').val(item.pos_start);
                    $('#tr_'+rel_id).find('td.pos_end').find('input').val(item.pos_end);
                    $('#tr_'+rel_id).find('td.line_num').find('input').val(item.line_num);
                }
            }
        })

        //保存表格-位置
        $('#save_tb').on('click',function () {
            if($('#excel_poses').val() == ''){
                layer.msg('请先选择要修改的模板');
                return false;
            }
            ajax('post','saveSortStdToolItemRel',$('#form-table').serialize());
        })
        //保存表格-排序
        $('#save_tb2').on('click',function () {
            ajax('post','',$('#form-table').serialize());
        })
    </script>
@endsection